package com.x.reader.controller;

import com.x.framework.Base;
import com.x.framework.model.ResData;
import com.x.reader.model.*;
import com.x.reader.service.AudioService;
import com.x.reader.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RestController
public class AudioController extends Base {

    @Autowired
    private AudioService audioService;

    @Autowired
    private HistoryService historyService;

    @RequestMapping(value = "/SelectAudioList")
    public ResData selectAudioList(Audio audio) {
        List<Audio> audioList = audioService.selectAudioList(audio);
        ResData resData = new ResData(1);
        resData.setResult(audioList);
        return resData;
    }

    @RequestMapping(value = "/SelectAudioChapterList")
    public ResData selectAudioChapterList(AudioChapter audioChapter) {
        List<AudioChapter> audioChapterList = audioService.selectAudioChapterList(audioChapter);
        ResData resData = new ResData(1);
        resData.setResult(audioChapterList);
        return resData;
    }

    @RequestMapping(value = "/SelectAudioChapter")
    public ResData selectAudioChapter(AudioChapter audioChapter, String userId) {
        List<AudioChapter> audioChapterList = audioService.selectAudioChapterList(audioChapter);
        audioChapter = audioChapterList.get(0);

        if (Base.isString(userId)) {
            History history = new History();
            history.setUserId(userId);
            history.setHistoryId(audioChapter.getAudioChapterId());
            history.setHistoryTitle(audioChapter.getAudioChapterTitle());
            history.setHistoryCover(audioChapter.getAudioChapterCover());
            history.setHistoryCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            history.setCategoryId(19);
            historyService.insertHistory(history);
        }

        ResData resData = new ResData(1);
        resData.setResult(audioChapter);
        return resData;
    }

}
